Thread: No operator matches "[]" these operands And error C2248

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    1

    No operator matches "[]" these operands And error C2248

    I'm getting No operator matches "[]" these operands

    and this error that I have no clue about :\program files (x86)\microsoft visual studio 10.0\vc\include\fstream(1116): error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

    Code:
    #include <string>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #define SIZE 20    
    using namespace std ; 
    struct Students{
        ifstream Fin;
        ofstream Fout;
        float Units, GPA, Total_Units, Avg_GPA, Mega_Units, Cyan, Mega_Cyan, GradeK; 
        long  ID;
        string  First_Name, Last_Name , name, letter, Grade;
    };
    
    void print_OList(Students List[], int size);
    void print_O(Students Temp, int size);
    float assign_grade(Students &Temp, int size);
    void Process_List(Students List[], int size, float Cyan);
    void Read_Stu(Students &Temp, ifstream &Fin, int size);
    void Calc_GPA(Students &Temp) ;
    int read_O(Students List[], int safe);
    void Intro();
    int main() {
        Students    List[SIZE];
        string letter;
        float Cyan = 0;
        int    size = 0, j = 0;
        Intro();
        size = read_O(List,SIZE);
        if (letter == "O" || letter == "o")
                {
                read_O(List, SIZE);
                Process_List(List, size, Cyan);
                print_OList(List, size);
                print_O(List[j], size);
        }
        return 0;
    }
    
        
    void Intro()
     {
        
        string letter, Option;
        do {
        cout << "'O' -- Open an existing student’s file." << endl;
        cout << "'A' -- Add new person to the current list." << endl;
        cout << "'R' -- Remove a person from the current list." << endl;
        cout << "'P' -- Print one student’s info in the current list." <<endl;
        cout << "'E' -- Edit information for 1 person." << endl;
        cout << "'S' -- Search." <<endl;
        cout << "'T' -- Sort the list" <<endl;
        cout << "'X' -- Exit the program." <<endl;
        cout << "Please choose an action or enter Z to exit the program: " << endl;
        cin >> letter;
        }while( Option != "z" || Option != "Z");
        
    }
    
    
    
    
    int read_O(Students List[], int safe, int size)
    {
        int        i = 0;
        ifstream    Fin ;
        string    name ;
        
        cout <<"Enter input file name: ";
        getline(cin, name);
        Fin.open(name.c_str());
        if(Fin.fail())
            cout << "Bad file.\n" ;
        else
        { 
            while (!Fin.eof())
              {
                 i++;
                if(i < SIZE)
                    Read_Stu(List[i], Fin, size);
                else 
                {
                    cout <<"Array is full.\n\n";
                    break;
                }
        
            }
        }
        return i ;
    }
    
    void Read_Stu(Students &Temp, ifstream &Fin, int size)
    {
        getline(Fin, Temp.name) ;
        Fin >> Temp.ID >> Temp.Grade ;
        Fin >> Temp.Units;
        assign_grade(Temp, size);
        Fin.ignore(10,'\n');
        
    }
    
    void print_OList(Students List[], int size)
    {
        int j = 0;
    
        cout << "\t" << endl;
        cout << "Name" << setw(23) << "ID#" << setw(15) << "Units" << setw(15) << "GPA" << endl;
        cout << "==========================================================" << endl;
    }
        
    void print_O(Students Temp, int size)
    {
        for (int j = 0 ; j < size ; j++)
        {
            cout << fixed << left << setw(23) << Temp.name << " " << setw(13) << Temp.ID << " " << setw(13) << setprecision(0) << Temp.Total_Units << " " << setprecision(2) << Temp.GPA << endl;
        }
        cout << "\t" << endl;
        cout << "==========================================================" << endl;
        cout << "Group of 6 Student's Totals " << Temp.Mega_Units << Temp.Avg_GPA << endl;
     }
    
    void Process_List(Students List[], int size, float Cyan)
        {
            for (int i = 0 ; i < size; i++)
            Calc_GPA(List[i]);
        }
    
    
        
    void Calc_GPA(Students &Temp) 
    { 
                  int i = 0;
    
                  Temp.Total_Units[i] += Temp.Units[i];
                  Temp.Cyan[i] += (Temp.GradeK[i] * Temp.Units[i]);
                  if (Temp.Total_Units[i] < 0)
                   {   
                       Temp.GPA = 0;
                   }
                   else if (Temp.Total_Units > 0)
                   {  
                       Temp.GPA = (Temp.Cyan)/Temp.Total_Units;
                       Temp.Mega_Units += Temp.Total_Units;
                       Temp.Mega_Cyan += (Temp.GPA * Temp.Total_Units);
                       Temp.Total_Units = 0;
                       Temp.Cyan = 0;
                  }
    }  
    
    
    float assign_grade(Students &Temp, int size)
    {    
        
                    for(int i = 0 ; i < size ; i++) 
                    {
                        while (Temp[i].Grade != "*")
                    {
                     if (Temp.Grade == "A" || Temp.Grade == "a")
                        Temp[i].GradeK = 4;
                    else if (Temp.Grade == "B" || Temp.Grade == "b")
                        Temp.GradeK = 3;
                    else if (Temp.Grade == "C" || Temp.Grade == "c")
                        Temp.GradeK = 2;
                    else if (Temp.Grade == "D" || Temp.Grade == "d")
                        Temp.GradeK = 1;
                    else if (Temp.Grade == "F" || Temp.Grade == "f")
                        Temp.GradeK = 0;
                
                    if (!(Temp.Grade == "A" || Temp.Grade == "a" || Temp.Grade == "B" || Temp.Grade == "b" || Temp.Grade == "C" || Temp.Grade == "c" || Temp.Grade == "D" || Temp.Grade == "d" || Temp.Grade == "F" || Temp.Grade == "f"))
                       { 
                         Temp.GradeK = 0;
                         Temp.Units = 0;
                        }
                   else if (Temp.Units < 0 || Temp.Units > 5)
                   {  
                       Temp.Units = 0;
                   }
                   
                    }
                    }
    }       
    
    
    
    //'O' (END)//

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    No Convention followed... Somewhere your variable and functions is starting with small case somewhere capital!

    In this function "print_O(List[j], size);" You are passing first argument by value, hence copy constructor invoked.
    Your structure has string stream objects and copying string stream is not allowed in C++.

    why-copying-stringstream-is-not-allowed

    Other mistakes:
    Check the signature of read_O().
    None of these are array "float Units, GPA, Total_Units, Avg_GPA, Mega_Units, Cyan, Mega_Cyan, GradeK;" But you have used it as some where as array and some where not. Why? I dont see any need of yours to use it as array!

    Missing thing:
    Consistency
    S_ccess is waiting for u. Go Ahead, put u there.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Fin and Fout seem out of place as member variables for your Student struct, you don't really seem to need them at all since you use other local variables to do all of your file I/O. Getting rid of them from the struct would help with what Maven said about the copying of stream objects.




    Code:
    while (!Fin.eof())
    {
        i++;
        if(i < SIZE)
            Read_Stu(List[i], Fin, size);
        else
        {
            cout <<"Array is full.\n\n";
            break;
        }
    }
    Read: FAQ > Why it's bad to use feof() to control a loop
    The topic in the link goes to examples in C but the lesson to be learned applies to C++ as well. You should overload the stream extraction operator (operator>>) for your Student struct and then you could simplify that whole loop to something like:
    Code:
    while( i < SIZE && Fin >> List[i] ) ++i;




    Code:
    int main() {
        Students    List[SIZE];
        string letter;
        float Cyan = 0;
        int    size = 0, j = 0;
        Intro();
        size = read_O(List,SIZE);
        if (letter == "O" || letter == "o")
        {
            read_O(List, SIZE);
            Process_List(List, size, Cyan);
            print_OList(List, size);
            print_O(List[j], size);
        }
        return 0;
    }
    
    void Intro()
     {
        
        string letter, Option;
        do {
        cout << "'O' -- Open an existing student’s file." << endl;
        cout << "'A' -- Add new person to the current list." << endl;
        cout << "'R' -- Remove a person from the current list." << endl;
        cout << "'P' -- Print one student’s info in the current list." <<endl;
        cout << "'E' -- Edit information for 1 person." << endl;
        cout << "'S' -- Search." <<endl;
        cout << "'T' -- Sort the list" <<endl;
        cout << "'X' -- Exit the program." <<endl;
        cout << "Please choose an action or enter Z to exit the program: " << endl;
        cin >> letter;
        }while( Option != "z" || Option != "Z");
        
    }
    Your letter variable in main is uninitialized. It looks like you mean to have the variable altered by a call to your Intro function but that function doesn't alter the letter variable in main, only the one local to the Intro function itself. In fact, the Intro function looks rather broken, it should probably loop until the option choosen by the user is not one of the acceptable answers and then return that answer back to the calling function (main).




    You have functions stating they return a value but do not (assign_grade) and you have functions that return a value but the value is not used by the calling function (read_O).




    You should ditch the fixed arrays and move towards using a vector of Student structs instead.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the operator "="error in class "String"
    By boyhailong in forum C++ Programming
    Replies: 9
    Last Post: 07-27-2011, 08:39 PM
  2. Replies: 6
    Last Post: 08-17-2009, 09:43 AM
  3. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  4. Replies: 9
    Last Post: 03-30-2009, 06:37 PM
  5. An error "invalid combination of operands and opcodes"!!
    By praseodeveloper in forum C++ Programming
    Replies: 4
    Last Post: 10-20-2005, 01:00 AM